package penguin.masking.asm;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

/**
 * @author wuxh
 * @version 1.0.0
 */
public class MaskingObjectMethodVisitor extends CustomMethodVisitor {

    @Override
    public void afterMethod() {
        //返回命令
        if (lvs != null) {
            Type[] argumentTypes = Type.getArgumentTypes(agent.getDescriptor());
//                    System.out.println(returnType.getClassName() + ":" + argumentTypes[0].getClassName());
            int id = lvs.newLocal(Type.getType("Ljava/lang/Object;"));
            mv.visitVarInsn(Opcodes.ASTORE, id);

            int id2 = lvs.newLocal(Type.getType("Ljava/sql/ResultSet;"));
            mv.visitVarInsn(ALOAD, 0);
            mv.visitTypeInsn(CHECKCAST, "java/sql/ResultSet");
            mv.visitVarInsn(ASTORE, id2);

            int id3 = lvs.newLocal(Type.getType("Ljava/lang/Object;"));
            if ("int".equals(argumentTypes[0].getClassName())) {
                mv.visitVarInsn(ILOAD, 1);
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
            } else {
                mv.visitVarInsn(ALOAD, 1);
            }
            mv.visitVarInsn(ASTORE, id3);

            mv.visitVarInsn(ALOAD, id2);
            mv.visitVarInsn(ALOAD, id3);
            mv.visitVarInsn(ALOAD, id);
            mv.visitMethodInsn(INVOKESTATIC, "penguin/masking/DataMaskingService", "maskingObject", "(Ljava/sql/ResultSet;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", false);
        }
    }
}
